package com.cms.bean;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;


public class MenuBean {
	private int id;
	private String menuName;
	private String menuCommand;
	private String menuPage;
	private int orderId;
	private int levelId;
	private int parentId;
    private int folderId;
    private List<MenuBean> children;
    private MenuBean parentBean;
    
    public MenuBean(){
    	children = new ArrayList<MenuBean>();
    }
    
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String getMenuCommand() {
		return menuCommand;
	}
	public void setMenuCommand(String menuCommand) {
		this.menuCommand = menuCommand;
	}
	public String getMenuPage() {
		return menuPage;
	}
	public void setMenuPage(String menuPage) {
		this.menuPage = menuPage;
	}
	public int getOrderId() {
		return orderId;
	}
	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}
	public int getLevelId() {
		return levelId;
	}
	public void setLevelId(int levelId) {
		this.levelId = levelId;
	}
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	public int getFolderId() {
		return folderId;
	}
	public void setFolderId(int folderId) {
		this.folderId = folderId;
	}
    
	public List<MenuBean> getChildren() {
		return children;
	}

	public void setChildren(List<MenuBean> children) {
		this.children = children;
	}

	public MenuBean getParentBean() {
		return parentBean;
	}

	public void setParentBean(MenuBean parentBean) {
		this.parentBean = parentBean;
	}

	public boolean addChildToParent(MenuBean child) {
		if (child.getParentId() == 0 || this.id == child.getParentId()) {
			child.setLevelId(this.getLevelId() + 1);
			int idx = 0;
			for (MenuBean node : children) {
				if (child.getOrderId() < node.getOrderId()) {
					break;
				} else {
					idx++;
					continue;
				}
			}

			child.setParentBean(this);
			children.add(idx, child);
			return true;
		} else if (children != null) {
			for (MenuBean e : children) {
				if (e.addChildToParent(child)) {
					return true;
				}
			}
		}

		return false;
	}
	
	public MenuBean getNodeById(int id){
		if(id ==0 || this.id == id)
	        return this;
		MenuBean  node = null;
		for(MenuBean e:this.children){
			if(node!=null) break;
			node = e.getNodeById(id);
		}
		return node;
	}
	
	public List<MenuBean> getPathV(int id) {
		List<MenuBean> pV = new ArrayList<MenuBean>();
		MenuBean mBean = this.getNodeById(id);
		if(mBean==null) return null;
		pV.add(mBean);
		while (mBean != null && mBean.getLevelId() > 0) {
			mBean = this.getNodeById(mBean.getParentId());
			if (mBean != null)
				pV.add(mBean);
		}
		return pV;
	}
	
}
